namespace AutoTest.Core.Configuration
{
    using System.Reflection;
    using FileSystem;
    using log4net.Appender;
    using log4net.Config;
    using log4net.Core;
    using Messaging;
    using Microsoft.Practices.ServiceLocation;
    using Ninject.Core;
    using Ninject.Core.Behavior;
    using Ninject.Integration.Log4net;

    /// <summary>
    /// Bootstraps the AutoTest application...call this mother first
    /// </summary>
    public static class BootStrapper
    {
        static readonly StandardKernel _kernel;
        //ensures only run once...however, need to call initialize method to invoke this ctor
        static BootStrapper()
        {
            _kernel = new StandardKernel();
            //set up kernel to autoload from this assembly
            _kernel.Load(
                new Log4netModule(),
                new MessageModule(Assembly.GetExecutingAssembly()),
                new BootStrapModule(),
                new AutoModule(Assembly.GetExecutingAssembly()));
            //use basic console appender for now
            BasicConfigurator.Configure(new ConsoleAppender {Threshold = Level.Info});
        }

        public static void Load(IModule module)
        {
            _kernel.Load(module);
        }

        public static void Initialize()
        {
        }
    }

    public class BootStrapModule : StandardModule
    {
        public override void Load()
        {
            //adding the kernel to itself...used in testing the BootStrapping 
            Bind<IKernel>().ToConstant(Kernel);
            Bind<IServiceLocator>()
                .To<QuickNinjectServiceLocator>()
                .Using<SingletonBehavior>();
            Bind<IDirectoryWatcher>().To<DirectoryWatcher>();
            //Is this the right way to do things?...
            ServiceLocator.SetLocatorProvider(() => Kernel.Get<IServiceLocator>());
        }
    }
}